package ui.control
{
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	
	import loaderData.LoaderSys;
	
	import texture.BitmapEx;
	
	import ui.GuiEnum;

	/**
	 * 按扭 
	 * @author Administrator
	 * 
	 */	
	public class GuiButton extends GuiStatic
	{
		protected var buttonImgList:Vector.<BitmapEx>;
		protected var buttostar:int;
		
		public function GuiButton()
		{
			
			super();
			
			img = new BitmapEx(null);
			addChildAt(img, 0);
			
			star = GuiEnum.BTN_UP;
			
			addEventListener(MouseEvent.MOUSE_DOWN, onBtnMouseDwon);
			addEventListener(MouseEvent.MOUSE_UP, onBtnMouseUp);
			addEventListener(MouseEvent.MOUSE_OVER, onBtnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onBtnMouseOut);
		}
		
		override public function init(xml:XML):void
		{
			if(xml.@text)
				text = xml.@text;
			
			if(xml.@x)
			{
				this.x = xml.@x;
			}
			
			if(xml.@y)
			{
				this.y = xml.@y;
			}
			
			if(xml.@text_x)
			{
				this.textField.x = xml.@text_x;
			}
			
			if(xml.@text_y)
			{
				this.textField.y = xml.@text_y;
			}
			
			if(xml.@width)
			{
				this.nWidth = xml.@w;
			}
			
			if(xml.@height)
			{
				this.nHeight = xml.@height;
			}
			
			if(xml.@img)
			{
				var tex:BitmapData = LoaderSys.inst.getBitmapDataByClass(xml.@img);
				
				if(tex) imgList = BitmapEx.getButtonImgList(tex);
			}
		}
		
		protected function onBtnMouseOut(event:MouseEvent):void
		{
			star = GuiEnum.BTN_UP;
		}
		
		protected function onBtnMouseOver(event:MouseEvent):void
		{
			star = GuiEnum.BTN_OVER;
		}
		
		protected function onBtnMouseUp(event:MouseEvent):void
		{
			star = GuiEnum.BTN_UP;
		}
		
		protected function onBtnMouseDwon(event:MouseEvent):void
		{
			star = GuiEnum.BTN_DOWN;
		}
		
		public function get star():int
		{
			return buttostar;
		}
		
		public function set star(value:int):void
		{
			buttostar = value;
			
			updateState(buttostar);
		}
		
		
		protected function updateState(n:int):void
		{
			if(imgList && img)
				this.img.bitmapData = this.buttonImgList[n].bitmapData;
		}

		override public function set backImg(value:BitmapData):void
		{
			
		}
		
		public function set imgList(value:Vector.<BitmapEx>):void
		{
			if(value && value.length > 0)
			{
				buttonImgList = value;
				updateState(buttostar);
			}
		}



	}
}